﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Articles;
using VK.Comm.TransactSql;
using VK.Casemeet.Lines;
using System.Data;
using VK.Casemeet.Merchants;
using VK.Casemeet.Elites;
using VK.Casemeet.Albums;
using VK.Casemeet.Chats;
using VK.Comm;
using VK.Casemeet.Photos;
using System.Text;
using VK.Casemeet.Slides;
using VK.Casemeet.Links;

namespace VK.Casemeet.Web.Front
{
    public partial class AreaDefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Master as Site).GetCurrentAreaID() == 0)
            {
                Response.Redirect("/");
            }

            TopShop1.AreaUrl = (this.Master as Site).GetAreaUrl();
            TopShop1.ProvinceID = (this.Master as Site).provinceID;
            TopShop1.CityID = (this.Master as Site).cityID;

            if (!IsPostBack)
            {
                //联盟动态
                rptArticle.DataSource = ArticleBLL.GetArticleList(1, 5, new List<Condition>(){
                    new Condition { TableName = "Article", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "CategoryID", ConditionType = ConditionType.NumeralEqual, ConditionValue = 1, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Article].[Created]", SortMode.Desc);
                rptArticle.DataBind();

                //联盟特色活动线路


                //盟友国内最新活动线路
                rptLine02.DataSource = LineBLL.GetLineList(1, 6, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Line", FieldName = "DestinationProvinceID", ConditionType = ConditionType.NumeralNotEqual, ConditionValue = 3346, DbType = DbType.Int32 },
                    new Condition { TableName = "Line", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Line].[Created]", SortMode.Desc);
                rptLine02.DataBind();

                //盟友境外最新活动线路
                rptLine03.DataSource = LineBLL.GetLineList(1, 6, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Line", FieldName = "DestinationProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = 3346, DbType = DbType.Int32 },
                    new Condition { TableName = "Line", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Line].[Created]", SortMode.Desc);
                rptLine03.DataBind();

                //户外俱乐部
                rptShop0101.DataSource = ShopBLL.GetShopList(1, 3, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 1, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0101.DataBind();
                rptShop0102.DataSource = ShopBLL.GetShopList(4, 8, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 1, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0102.DataBind();

                //户外厂商
                rptShop0201.DataSource = ShopBLL.GetShopList(1, 3, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 2, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0201.DataBind();
                rptShop0202.DataSource = ShopBLL.GetShopList(4, 8, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 2, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0202.DataBind();

                //基地与景区
                rptShop0301.DataSource = ShopBLL.GetShopList(1, 3, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 3, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0301.DataBind();
                rptShop0302.DataSource = ShopBLL.GetShopList(4, 8, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 3, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                }, "[Shop].[Created]", SortMode.Desc);
                rptShop0302.DataBind();

                ////学生社团
                //rptShop04.DataSource = ShopBLL.GetShopList(1, 5, new List<Condition>(){
                //    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                //    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                //    new Condition { TableName = "Shop", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = 4, DbType = DbType.Int32 },
                //    new Condition { TableName = "Shop", FieldName = "State", ConditionType = ConditionType.BoolEqual, ConditionValue = 1, DbType = DbType.Boolean }
                //}, "[Shop].[Created]", SortMode.Desc);
                //rptShop04.DataBind();

                //联盟精英
                rptElite.DataSource = EliteBLL.GetEliteList(1, 6, new List<Condition>(){
                    new Condition { TableName = "Elite", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Elite", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 }
                }, "[Elite].[Created]", SortMode.Asc);
                rptElite.DataBind();

                //精彩活动图集
                rptAlbum.DataSource = AlbumBLL.GetShopAlbumList(1, 6, new List<Condition>(){
                    new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 }
                }, "[Album].[Created]", SortMode.Desc);
                rptAlbum.DataBind();

                //在线客服
                rptChat.DataSource = ChatBLL.GetChatList(new List<Condition>(){
                    new Condition { TableName = "Chat", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                    new Condition { TableName = "Chat", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 },
                    new Condition { TableName = "Chat", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Chat].[Sort]", SortMode.Asc);
                rptChat.DataBind();

                //合作伙伴
                rptLink.DataSource = ArticleBLL.GetArticleList(1, 5, new List<Condition>(){
                    new Condition { TableName = "Article", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralLittleThan, ConditionValue = 1, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "CategoryID", ConditionType = ConditionType.NumeralEqual, ConditionValue = 2, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Article].[Created]", SortMode.Desc);
                rptLink.DataBind();

                //户外保险
                rptBaoXian.DataSource = ArticleBLL.GetArticleList(1, 3, new List<Condition>(){
                    new Condition { TableName = "Article", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralLittleThan, ConditionValue = 1, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "CategoryID", ConditionType = ConditionType.NumeralEqual, ConditionValue = 3, DbType = DbType.Int32 },
                    new Condition { TableName = "Article", FieldName = "IsEnable", ConditionType = ConditionType.BoolEqual, ConditionValue = true, DbType = DbType.Boolean }
                }, "[Article].[Created]", SortMode.Desc);
                rptBaoXian.DataBind();

                TopGroup1.AreaID = (this.Master as Site).GetCurrentAreaID();
            }
        }

        public string GetSlideData(string code)
        {
            StringBuilder html = new StringBuilder();
            List<SlideEntity> slidelst = SlideBLL.GetSlideList(new List<Condition>(){ 
                new Condition { TableName = "Slide", FieldName = "Code", ConditionType = ConditionType.TextEqual, ConditionValue = code, DbType = DbType.String },
                new Condition { TableName = "Slide", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).provinceID, DbType = DbType.Int32 },
                new Condition { TableName = "Slide", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = (this.Master as Site).cityID, DbType = DbType.Int32 }
            }, null, SortMode.Asc);
            if (slidelst.Count > 0)
            {
                List<PhotoEntity> photolst = PhotoBLL.GetPhotoList(new List<Condition>(){ 
                    new Condition { TableName = "Photo", FieldName = "ObjectType", ConditionType = ConditionType.NumeralEqual, ConditionValue = 4, DbType = DbType.Int32 },
                    new Condition { TableName = "Photo", FieldName = "ObjectID", ConditionType = ConditionType.NumeralEqual, ConditionValue = slidelst[0].SlideID, DbType = DbType.Int32 }
                }, null, SortMode.Asc);
                for (int n = 0; n < photolst.Count; n++)
                {
                    if (n == photolst.Count - 1)
                    {
                        html.Append("{ title: '', desc: '', src: '/UserImages/Photos/")
                            .Append(photolst[n].ImgUrl).Append("', href: '").Append(photolst[n].JumpUrl)
                            .Append("', target: '" + PhotoHelper.GetTargetLabel(photolst[n].Target) + "' }");
                    }
                    else
                    {
                        html.Append("{ title: '', desc: '', src: '/UserImages/Photos/")
                            .Append(photolst[n].ImgUrl).Append("', href: '").Append(photolst[n].JumpUrl)
                            .Append("', target: '" + PhotoHelper.GetTargetLabel(photolst[n].Target) + "' },");
                    }
                }

            }
            return html.ToString();
        }
    }
}